package chapter3;

import Config.GlobalKafkaConfig;
import org.apache.kafka.clients.consumer.*;
import org.apache.kafka.common.PartitionInfo;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicBoolean;


public class CheckOffsetAndPosition {
    public static final String brokerList = GlobalKafkaConfig.brokerList;
    public static final String topic = GlobalKafkaConfig.topic;
    public static final String groupId = GlobalKafkaConfig.groupId;
    private static AtomicBoolean running = new AtomicBoolean(true);

    public static Properties initConfig() {
        Properties props = new Properties();
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,
                StringDeserializer.class.getName());
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
                StringDeserializer.class.getName());
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, brokerList);
        props.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
        return props;
    }

    public static void main(String[] args) {
        Properties props = initConfig();
        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);

        List<PartitionInfo> partitionInfoList = consumer.partitionsFor(topic);
        for (PartitionInfo partitionInfo : partitionInfoList) {
            System.out.println("partitionInfo: "+partitionInfo);
        }

        Map<String, List<PartitionInfo>> listMap = consumer.listTopics();
        for (Map.Entry<String, List<PartitionInfo>> entry : listMap.entrySet()) {
            System.out.println("===========================================");
            System.out.println(entry.getKey()+entry.getValue());
            System.out.println("===========================================");
        }



        TopicPartition tp = new TopicPartition(topic, 0);

        Map<TopicPartition, Long> topicPartitionLongMap = consumer.endOffsets(Arrays.asList(tp));

        for (Map.Entry<TopicPartition, Long> entry : topicPartitionLongMap.entrySet()) {
            System.out.println("##########################################");
            TopicPartition key = entry.getKey();
            Long value = entry.getValue();
            System.out.println(key.topic()+value);
            System.out.println("##########################################");
        }
        consumer.assign(Arrays.asList(tp));
        long lastConsumedOffset = -1;
        while (true) {
            ConsumerRecords<String, String> records = consumer.poll(1000);
            if (records.isEmpty()) {
                break;
            }
            List<ConsumerRecord<String, String>> partitionRecords
                    = records.records(tp);
            lastConsumedOffset = partitionRecords
                    .get(partitionRecords.size() - 1).offset();
            consumer.commitSync();//同步提交消费位移
        }
        System.out.println("comsumed offset is " + lastConsumedOffset);
        OffsetAndMetadata offsetAndMetadata = consumer.committed(tp);
        System.out.println("commited offset is " + offsetAndMetadata.offset());
        long posititon = consumer.position(tp);
        System.out.println("the offset of the next record is " + posititon);
    }
}
